﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mMachine.Common
{
    public class MailHelper
    {
        public static void SendMail(string[] strlist, string MAINTAIN_NO, string REPORT_PERSON, string REPORT_TIME, string MACHINE_NO,
            string MACHINE_DESC, string REPORT_STATE, string FAILURE_TYPE, string FAILURE_DESC,string MACHINE_NAME,string MACHINE_LOCATION,string PHONE)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            // msg.To.Add("Bill.Luo@lamex.com.hk");
            foreach (var item in strlist)
            {
                if (item.Length > 0)
                    msg.To.Add(item);
            }         
            msg.From = new MailAddress("mailsender@lamex.com.hk", "admin", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
            msg.Subject = "设备维修故障通知";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码             
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    
            string strBody = "";
            string strTitle = "Dear all,<br/>您收到一份待处理事项,请协助处理,谢谢！";
            string strwarnlevel= " <td style=width:500px;>一级（发生故障报告事件）</td> ";
            string strMAINTAIN_NO = " <td style=width:500px;>" + MAINTAIN_NO + "</td> ";
            string strREPORT_PERSON = " <td style=width:500px;>" + REPORT_PERSON + "</td> ";
            string strPHONE = " <td style=width:500px;>" + PHONE + "</td> ";
            string strREPORT_TIME = " <td style=width:500px;>" + REPORT_TIME + "</td> ";
            string strMACHINE_NO = " <td style=width:500px;>" + MACHINE_NO + "</td> ";
            string strMACHINE_DESC = " <td style=width:500px;>" + MACHINE_DESC + "</td> ";
            string strREPORT_STATE = " <td style=width:500px;>" + REPORT_STATE + "</td> ";
            string strFAILURE_TYPE = " <td style=width:500px;>" + FAILURE_TYPE + "</td> ";
            string strFAILURE_DESC = " <td style=width:500px;>" + FAILURE_DESC + "</td> ";
            string strMACHINE_NAME= " <td style=width:500px;>" + MACHINE_NAME + "</td> ";
            string strMACHINE_LOCATION = " <td style=width:500px;>" + MACHINE_LOCATION + "</td> ";

            strBody = strBody + strTitle;
            strBody = strBody + " <table  border=8 style=background-color:#CCE8EB;font-size:small;align:right> ";
            strBody = strBody + " <tr><td style=width:150px;>预警级别:        </td>" + strwarnlevel + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>维修编号:        </td>" + strMAINTAIN_NO + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>报修人:        </td>" + strREPORT_PERSON + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>报修人电话:        </td>" + strPHONE + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>报修时间:    </td>" + strREPORT_TIME + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>设备编号:</td>" + strMACHINE_NO + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>设备名称:</td>" + strMACHINE_NAME + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>设备位置:</td>" + strMACHINE_LOCATION + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>设备描述:</td>" + strMACHINE_DESC + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>报修状态:    </td>" + strREPORT_STATE + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>故障类别:    </td>" + strFAILURE_TYPE + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>故障描述:    </td>" + strFAILURE_DESC + "</tr> ";
            strBody = strBody + " </table> ";
            msg.Body = strBody;

            SmtpClient client = new SmtpClient();
            // client.Credentials = new System.Net.NetworkCredential("luoyuansheng@moptron.biz", "Luo7701167");
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //上述写你的GMail邮箱和密码    
            //client.Port = 25;//Gmail使用的端口  25  
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            try
            {
                // client.SendAsync(msg, userState);
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }
    }
}
